/*
 * Created on 2005-3-23
 *
 */
package com.hd123.rumba.action.impex;

import java.io.File;
import java.util.Iterator;
import java.util.Vector;

import com.hd123.rumba.impex.ImpexLog;
import com.hd123.rumba.impex.ImpexMgr;
import com.hd123.rumba.impex.ImpexXml;
import com.hd123.rumba.impex.Indicator;
import com.hd123.rumba.request.Action;
import com.hd123.rumba.request.ActionAbstractImpl;
import com.hd123.rumba.request.ActionConstants;
import com.hd123.rumba.request.FileTooLargeException;
import com.hd123.rumba.request.JspLib;

/**
 * @author caili
 */
public class ViewImpex extends ActionAbstractImpl implements Action {
  public String execute() throws Exception {
    String startnode = (String) dd.getFormValue("start");
    String url = ActionConstants.NOTIMPLEMENTED;
    if (startnode == null)
      startnode = "view";
    if (startnode.equals("view"))
      url = executeWithPermission("view", ImpexMgr.PERMISSION_IMPEX);
    else if (startnode.equals("dispatch")) {
      try {
        dd.prepareMultipartRequest();
      } catch (FileTooLargeException e) {
        dd.addErrorText(e.getMessage());
        return ActionConstants.FILETOOLARGE;
      }
      if (dd.getFormValues().containsKey("doImport"))
        url = executeWithPermission("doImport", ImpexMgr.PERMISSION_IMPEX);
      else if (dd.getFormValues().containsKey("doExport"))
        url = executeWithPermission("doExport", ImpexMgr.PERMISSION_IMPEX);
    }
    return url;
  }

  public String view() throws Exception {
    dd.setWorkingTemplate("rumba/impex/Impex.jsp");
    return ActionConstants.APPLICATIONFRAME;
  }

  public String doImport() throws Exception {
    File file = null;
    Vector v = null;
    Iterator keys = dd.createFormPrefixedKeysIterator("import_");
    while (keys.hasNext()) {
      String key = (String) keys.next();
      file = (File) dd.getFormValue("import_" + key);
      try {
        v = new ImpexXml().doImport(file, dd.getCurrentUser());
        for (int i = 0; i < v.size(); ++i) {
          ImpexLog log = (ImpexLog) v.get(i);
          dd.addHint(log.getMessage(Indicator.IMPORT));
        }
      } finally {
        file.delete();
      }
    }
    dd.setWorkingTemplate("rumba/impex/Impex.jsp");
    return ActionConstants.APPLICATIONFRAME;
  }

  public String doExport() throws Exception {
    File dir = new File(JspLib.getTempExportDir(dd.getRequest()));
    if (!dir.exists())
      dir.mkdirs();
    Iterator values = dd.createFormSelectedValuesIterator("export");
    while (values.hasNext()) {
      String objectName = (String) values.next();
      String fileName = objectName + new Double(java.lang.Math.random()).toString().substring(1) + ".xml";
      File file = new File(dir, fileName);
      ImpexLog log = new ImpexXml().doExport(objectName, file, dd.getCurrentUser());
      dd
          .addHint(log.getMessage(Indicator.EXPORT)
              + (log.getEndTime() == null ? "" : "<a href=\""
                  + JspLib.getStaticContent("tango/export/" + fileName) + "\">" + objectName
                  + "</a>"));
    }
    dd.setWorkingTemplate("impex/Impex.jsp");
    return ActionConstants.APPLICATIONFRAME;
  }
}
